<?php
class user
{
	public static function active()
	{
		if(!ds::get('user.status'))
		{
			$status = new stdclass();
			$status->login = false;
			$status->admin = false;
			$session = Session::instance();
			if($session->get('user'))
			{
				$status->login = true;
				if($session->get('user')->role == 'admin')
				{
					$status->admin = true;
				}
			}
			ds::set('user.status',$status);
		}
		return ds::get('user.status');
	}

	public static function get_user()
	{
		return Session::instance()->get('user');
	}

	public static function get_user_info($email)
	{
		if(!ds::get('user.infos'))
		{
			$user_path = Kohana::config('core.user_data_path').'users.php';
			$fp = fopen($user_path,'r');
			$user_infos = array();
			while(!feof($fp))
			{
				$user = fgets($fp,1024);
				if(substr($user,0,1)=='#' OR trim($user) == '') continue;
				$user = explode('|',$user);
				if($user[2] == $email)
				{
					$user_info = $user;
				}
				$user_infos[] = $user;
			}
			ds::set('user.infos',$user_infos);
			if(!empty($user_info))
			{
				$tmp = array('username','password','email');
				// has link
				if(count($user_info)==4)
				{
					$tmp[] = 'link';
				}
				return (object)array_combine($tmp,$user_info);
			}
		}
		$user_infos = ds::get('user.infos');
		if(count($user_infos))
		{
			foreach($user_infos as $user_info)
			{
				if($user_info[2] == $email)
				{
					$tmp = array('username','password','email');
					// has link
					if(count($user_info)==4)
					{
						$tmp[] = 'link';
					}
					return (object)array_combine($tmp,$user_info);
				}
			}
		}
		return false;
	}

	public static function register($data)
	{
		$user_data_path = Kohana::config('core.user_data_path').'users.php';
		$user_info = PHP_EOL.$data['username'].'|'.md5($data['password']).'|'.$data['email'];
		if(!empty($data['link']))
		{
			$user_info .= '|'.$data['link'];
		}
		file_put_contents($user_data_path,$user_info,FILE_APPEND);
	}

	public static function login()
	{
		$data = Input::instance()->post();
		$user_data_path = Kohana::config('core.user_data_path').'users.php';
		$fp = fopen($user_data_path,'r');
		$i = 0;
		while(!feof($fp))
		{
			$user = fgets($fp,1024);
			if(substr($user,0,1)=='#' OR trim($user)=='') continue;
			++$i;
			$user = explode('|',$user);
			if(!empty($user[2]) AND $user[2] == $data['email'])
			{
				if(md5($data['password']) == $user[1])
				{
					$user_object = new stdclass();
					$user_object->username = $user[0];
					$user_object->email = $user[2];
					$user_object->link = '';
					empty($user[3]) OR $user_object->link = $user[3];
					$user_object->role = 'login';
					if($i===1)
					{
						$user_object->role = 'admin';
					}
					Session::instance()->set('user',$user_object);
					return true;
				}
			}
		}
		return self::site_login($data);
	}

	public static function site_login($data)
	{
		$login_url = Kohana::config('site.login_url');
		$result = json_decode(post_request($login_url,$data),true);
		if(empty($result['status']) OR $result['status']!='ok')
		{
			return false;
		}
		$user_object = new stdclass();
		if(empty($result['username']))
		{
			$result['username'] = 'user';
		}
		$user_object->username = $result['username'];
		if(empty($result['link']))
		{
			$result['link'] = '';
		}
		$user_object->link = $result['link'];
		$user_object->role = 'login';
		$user_object->email = $data['email'];
		Session::instance()->set('user',$user_object);
		$user_info = array_merge($result,$data);
		self::register($user_info);
		return true;
	}

	public function logout()
	{
		Session::instance()->destroy();
	}

	public static function vote($article_id)
	{
		$article_id = (int)$article_id;
		$vote_file = Kohana::config('core.article_data_path').$article_id.'.vote';
		file_put_contents($vote_file,user::get_user()->email.PHP_EOL,FILE_APPEND);
		$meta_file = Kohana::config('core.article_data_path').$article_id.'.meta';
		$content = @file_get_contents($meta_file);
		$meta = explode('|',$content);
		$meta[4] += 1;
		file_put_contents($meta_file,implode('|',$meta));
	}

	public static function has_voted($article_id)
	{
		$article_id = (int)$article_id;
		if(user::active()->login)
		{
			$vote_file = Kohana::config('core.article_data_path').$article_id.'.vote';
			if(!file_exists($vote_file)) return false;
			$votes = explode(PHP_EOL,file_get_contents($vote_file));
			if(in_array(user::get_user()->email,$votes))
			{
				return true;
			}
		}
		return false;
	}

	public static function get_emails()
	{
		$user_data_path = Kohana::config('core.user_data_path').'users.php';
		$users = @file_get_contents($user_data_path);
		if($users)
		{
			$users = explode(PHP_EOL,$users);
			$emails = array();
			foreach($users as $user)
			{
				if(!empty($user[2]))
				{
					$emails[] = $user[2];
				}
			}
			return $emails;
		}
		return array();
	}

}
